package com.ruoyi.web.rmNew.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.ruoyi.web.core.config.ConfigLoader;
import com.ruoyi.web.core.config.Rm2Config;
import com.ruoyi.web.rmNew.service.http.ObHttpService2;
import com.ruoyi.web.rmNew.service.http.RmHttpService2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ObUpdateService {
    @Autowired
    private Rm2Config config;
    @Autowired
    private ObHttpService2 obHttpService2;
    @Autowired
    private ConfigLoader configLoader;

    @Scheduled(fixedDelayString = "5000")
    public void scheduleTask() {
        log.info("开始更新 updateObGameDetail----");
        ObUpdateService service = SpringUtil.getBean(ObUpdateService.class);
        service.updateObGameDetail();
    }

    public void updateObGameDetail() {
        String obPath = configLoader.getRmPath();
        String obGamePath = obPath + "ob-game.txt";
        String obGameDetailPath = config.getRmPath() + "ob-gamescore";
        List<String> games = FileUtil.readLines(obGamePath, "utf8");
        for (int i = 0; i < games.size(); i++) {
            String game = games.get(i);
            String[] gameInfo = game.split("\t");
            String gameid = gameInfo[0];

            String path = obGameDetailPath + File.separator + gameid + ".txt";

            boolean isRead = true;
            boolean exist = FileUtil.exist(path);
            if (exist) {
                long lastModifiedTime = new File(path).lastModified();
                isRead = (System.currentTimeMillis() - lastModifiedTime) > (5 * 60 * 1000);
            }

            if (isRead) {
                log.info("{}, 是否读取:{}", game, isRead);
                obHttpService2.sendObDetail(gameid, path);
            }
        }
    }

    public void showObUpdateTime() {
        String rmPath = configLoader.getRmPath();
        String rmGamePath = rmPath + "rm-game.txt";
        String rmGameDetailPath = config.getRmPath() + "gamenames" + File.separator + "rm-gamescore";
        List<String> games = FileUtil.readLines(rmGamePath, "utf8");

        List<String> gameUpdateList = new ArrayList<>();
        for (int i = 0; i < games.size(); i++) {
            String game = games.get(i);
            String[] gameInfo = game.split("\t");
            String gameid = gameInfo[0];

            String path = rmGameDetailPath + File.separator + gameid + ".txt";

            boolean isRead = true;
            boolean exist = FileUtil.exist(path);
            String updateTime = "无";
            if (exist) {
                long lastModifiedTime = new File(path).lastModified();
                isRead = (System.currentTimeMillis() - lastModifiedTime) < (5 * 60 * 1000);
                updateTime = DateUtil.format(new Date(lastModifiedTime), "HH:mm");
            }
            String msg = StrUtil.format("{}\t存在:{}\t{}\t{}", game, exist, updateTime, isRead);
            gameUpdateList.add(msg);
        }
        FileUtil.writeLines(gameUpdateList, config.getRmPath() + "ob-update.txt", "utf8");
    }


}
